layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
    var ptype = $("#pageType").prop("title");
    //新闻列表
    var tableIns = table.render({
        elem: '#infoList',
        url : '/iprint/admin/getInfoList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [10,15,20,25],
        id : "infoList",
        request:{
            pageName: 'currPage' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{
            queryType:ptype
        },
        response: {
            statusName: 'status' //数据状态的字段名称，默认：code
            ,statusCode: 200 //成功的状态码，默认：0
            ,countName: 'totalCount' //数据总数的字段名称，默认：count
            ,dataName: 'list' //数据列表的字段名称，默认：data
        },
        cols : [[
            {type: "checkbox",  fixed:"left",width:50},
            {field: 'id', 		title: 'ID',width:60, align:"center"},
            {field: 'title', 	title: '文章标题',},
            {field: 'state', 	title: '文章状态',  	align:'center',templet:"#infoStatus"},
            {field: 'category', title: '分类', 	align:'center',templet:"#infoType"},
            {field: 'top', 		title: '是否置顶', 	align:'center', templet:function(d){
                    if(d.top =='1'){
                        return '<span class="layui-red">置顶中</span>'
                    }else{
                        return '<span class="layui-red">未置顶</span>'
                    }
                }},
            {field: 'createTime', title: '发布时间', align:'center', minWidth:110},
            {field: 'updateTime', title: '更新时间', align:'center',minWidth:110},
            {title: '操作', width:170, templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
    });


    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("infoList",{
                page: {
                    currPage: 1 //重新从第 1 页开始
                },
                where:{key:$(".searchVal").val()},
                url : '/iprint/admin/getInfoList'
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加文章
    function addNews(edit){
        var index = layui.layer.open({
            title : "添加文章",
            type : 2,
            content : "addInfoPage",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".title").val(edit.title);
                    body.find(".infoid").val(edit.id);
                    body.find(".infodesc").val(edit.infodesc);
                    body.find("#info_content").val(edit.content);
                    body.find(".categoryaa input[value='"+edit.category+"']").prop("checked","checked");
                    body.find(".top input[name='top'][value='"+edit.top+"']").prop("checked","checked");
                    body.find(".infoStatus option[value='"+edit.state+"']").prop("selected","selected");
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })


        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    //删除文章
    $(".addNews_btn").click(function(){
        addNews();
    })
    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            $.ajax({
                url:"/iprint/admin/getInfo?id="+data.id,
                type:"GET",
                dataType:"json",
                success:function(msg){
                    addNews(msg);
                }
            });
        } else if(layEvent === 'del'){ //删除
            layer.msg('确定删除此条信息？', {
                time: 0 //不自动关闭
                ,btn: ['确定', '取消']
                ,yes: function(index){
                    layer.close(index);
                    $.ajax({
                        url:"/iprint/admin/delInfo?id="+data.id,
                        type:"GET",
                        dataType:"json",
                        success:function(msg){
                        }
                    });
                    tableIns.reload();
                }
            });
        } else if(layEvent === 'look'){ //预览
            layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
        }
    });

})